using System;
using System.ComponentModel;
using Microsoft.SqlServer.Management.Smo;

namespace SsmsAddin
{
    internal class MyExtendProperty : INotifyPropertyChanged
    {
        #region Implementation of INotifyPropertyChanged

        public MyExtendProperty()
        {
        }
        
        public MyExtendProperty(SqlSmoObject smoObject, string name, string objectName, string value)
            : base()
        {
            SmoObject = smoObject;
            _name = name;
            ObjectName = objectName;
            _value = value;
        }



        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public SqlSmoObject SmoObject;
        private string _name;
        private string _value;

        public string Name
        {
            get { return _name; }
            set
            {
                if (!UcExtendProperties.Initializing)
                {
                    if (typeof (Table) != SmoObject.GetType()) return;

                    var smoObject = (Table) SmoObject;

                    if (_name == null || smoObject.ExtendedProperties[_name] == null)
                    {
                        smoObject.ExtendedProperties.Add(new ExtendedProperty(smoObject, value, _value));
                    }
                    else
                    {
                        smoObject.ExtendedProperties[_name].Name = value;
                    }
                    smoObject.Alter();

                }
                _name = value;
                OnPropertyChanged("Name");
            }
        }
        public string ObjectName { get; set; }

        public string Value
        {
            get { return _value; }
            set
            {
                _value = value;
                if (UcExtendProperties.Initializing) return;

                if (typeof (Table) == SmoObject.GetType())
                {
                    var smoObject = (Table) SmoObject;
                    if (smoObject.ExtendedProperties[_name] == null)
                    {
                        smoObject.ExtendedProperties.Add(new ExtendedProperty(smoObject, _name, _value));
                    }
                    else
                    {
                        smoObject.ExtendedProperties[_name].Value = _value;
                    }
                    smoObject.Alter();
                }
                else if (typeof (Column) == SmoObject.GetType())
                {
                    var parent = (Column) SmoObject;
                    if (parent.ExtendedProperties[_name] == null)
                    {
                        parent.ExtendedProperties.Add(new ExtendedProperty(parent, _name, _value));
                    }
                    else
                    {
                        parent.ExtendedProperties[_name].Value = _value;
                    }
                    parent.Alter();
                }
                OnPropertyChanged("Value");
            }
        }

        private void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }


        public void RemoveMe()
        {
            if (typeof(Table) != SmoObject.GetType() && typeof(Column) != SmoObject.GetType()) return;
            if (SmoObject == null) return;
            var extendProperties = typeof(Table) == SmoObject.GetType() ? ((Table)SmoObject).ExtendedProperties : ((Column)SmoObject).ExtendedProperties;
            extendProperties[_name].Drop();

            if (typeof(Table) == SmoObject.GetType())
            {
                ((Table) SmoObject).Alter(); 
            }
            else
            {
                ((Column)SmoObject).Alter();
            }
        }

        protected MyExtendProperty CurrentExtentProperty
        {
            get; set;
        }
    }
}